//
//  QSHeaderSearchView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/4/21.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSHeaderSearchView.h"

@implementation QSHeaderSearchView
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configSubView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}
- (void)configSubView{
    [self addSubview:self.whiteBgView];
    [self.whiteBgView addSubview:self.searchTextField];
    [self.whiteBgView addSubview:self.rightButton];
    
    [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView).offset(kRealValue(10));
        make.right.equalTo(self.whiteBgView).offset(- kRealValue(64));
        make.bottom.equalTo(self.whiteBgView).offset(- kRealValue(7));
        make.height.offset(kRealValue(30));
    }];
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_searchTextField);
        make.right.equalTo(self.whiteBgView);
        make.width.offset(kRealValue(64));
        make.height.equalTo(_searchTextField);
    }];
}

#pragma mark - Lazy Load
- (UIView *)whiteBgView{
    if (!_whiteBgView) {
        _whiteBgView = [[UIView alloc]initWithFrame:self.frame];
        _whiteBgView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    }
    return _whiteBgView;
}

- (UITextField *)searchTextField{
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc]init];
        _searchTextField.borderStyle = UITextBorderStyleNone;
        _searchTextField.font = [UIFont QSFontOfSize14];
        _searchTextField.layer.cornerRadius = 5;
        _searchTextField.layer.borderWidth = 0.5;
        _searchTextField.layer.borderColor = [UIColor QSColorGrayEAEAEA].CGColor;
        _searchTextField.placeholder = @" 请输入企业名称";
        _searchTextField.delegate = self;
    }
    return _searchTextField;
}

- (UIButton *)rightButton{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setTitle:@"取消" forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont QSFontOfSize14];
        [_rightButton setTitleColor:[UIColor QSColorBlack434343] forState:UIControlStateNormal];
        [self.rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (void)rightButtonAction:(UIButton *)sender{
    if ([sender.currentTitle isEqualToString:@"取消"]) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(headerSearchViewDidClickBack:)]) {
            [self.delegate headerSearchViewDidClickBack:self];
        }
    }else{
        if (self.delegate && [self.delegate respondsToSelector:@selector(headerSearchViewDidClickClear:)]) {
            [self.delegate headerSearchViewDidClickClear:self];
        }
    }
}
//收起键盘开始搜索
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length > 0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(headerSearchViewDidSearchAction:)]) {
            [self.delegate headerSearchViewDidSearchAction:self];
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //禁止输入空格
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    if (![string isEqualToString:tem]) {
        return NO;
    }
    NSInteger lenth = textField.text.length + string.length;
    if (lenth > 0) {
        [self.rightButton setTitle:@"清空" forState:UIControlStateNormal];
    }else{
        [self.rightButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    NSLog(@"%lu--%lu", (unsigned long)string.length, (unsigned long)textField.text.length);//输入文字时 一直监听
    return YES;
}
//换行变搜索
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"7");// 点击键盘的‘换行’会调用
    [textField resignFirstResponder];
    return YES;
}

/**
*  键盘即将显示的时候调用
*/
- (void)keyboardWillShow:(NSNotification *)note{
    [[UIViewController currentViewController] keyboardWillShow:note TextY:0];
}
/**
 *  键盘即将退出的时候调用
 */
- (void)keyboardWillHide:(NSNotification *)note{
    [[UIViewController currentViewController] keyboardHiden:note];
}
- (void)dealloc{
    //移除通知
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
